#!/bin/bash
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@

prog=t240ms
pfix=z${prog}
loc=${pfix}
tmp=/tmp/${pfix}
destgdb=tools/dest-gdb-7.7/bin/gdb
gdb=/opt/home/${destgdb}

if [ ${gdb} = nogdb ]; then
	echo "gdb is out of date - exiting test"
	exit 0
fi

echo "using ${gdb}"

# cleanup
rm -f ${loc}.pid ${loc}.core.*
rm -f ${tmp}.in.* ${tmp}.out.* ${tmp}.err.*

# run -core
goshell31 ${prog} $*

# post-process core-file
pid=`cat ${loc}.pid`
unset PYTHONPATH
${gdb} -n ${prog} ${loc}.core.$pid > ${tmp}.out.p.$pid 2> ${tmp}.err.p.$pid <<eof
source ../../../gdb/sb.gdb
psbover
quit
eof

# get status-file
statusfile=`grep -w 'TEST_STOP' ms.env | sed 's|TEST_STOP=||'`

# check for errors
# Filter out these two lines
# Missing separate debuginfo for
# Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/85/756cec1977a6d3ae65be87c2878591481668d7
cnt=`cat ${tmp}.err.p.$pid | egrep -v '(read pathname|^$|^warning:|^Missing|^Try)' | wc -l`
if [ $cnt != 0 ]; then
	echo "gdb errors"
	cat ${tmp}.err.p.$pid
	rm -f $statusfile
else
	touch $statusfile
fi
rm -f ${loc}.pid ${loc}.core.*
rm -f ${tmp}.in.* ${tmp}.out.* ${tmp}.err.*

# do tcheck
tcheck ${prog}
